//
//  WCNDashBorder.m
//  WCN
//
//  Created by 杨鑫 on 15/6/12.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNDashBorder.h"
//
#import "Macros.h"

@implementation WCNDashBorder

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextBeginPath(context);
    
    CGContextSetLineWidth(context, kOnePX);
    CGContextSetStrokeColorWithColor(context, self.borderColor.CGColor);
    CGFloat lengths[] = {4, 2};
    CGContextSetLineDash(context, 0, lengths, 2);
    
    if (_leftBorder) {
        CGPoint start = CGPointPathChange(CGPointMake(0, 0), kLineVertical, 1);
        CGPoint end = CGPointPathChange(CGPointMake(0, rect.size.height), kLineVertical, 1);
        
        CGContextMoveToPoint(context, start.x, start.y);
        CGContextAddLineToPoint(context, end.x, end.y);
    }
    
    if (_rightBorder) {
        CGPoint start = CGPointPathChange(CGPointMake(rect.size.width, 0), kLineVertical, -1);
        CGPoint end = CGPointPathChange(CGPointMake(rect.size.width, rect.size.height), kLineVertical, -1);
        
        CGContextMoveToPoint(context, start.x, start.y);
        CGContextAddLineToPoint(context, end.x, end.y);
    }
    
    if (_topBorder) {
        CGPoint start = CGPointPathChange(CGPointMake(0, 0), kLineHorizontal, 1);
        CGPoint end = CGPointPathChange(CGPointMake(rect.size.width, 0), kLineHorizontal, 1);
        
        CGContextMoveToPoint(context, start.x, start.y);
        CGContextAddLineToPoint(context, end.x, end.y);
    }
    
    if (_bottomBorder) {
        CGPoint start = CGPointPathChange(CGPointMake(0, rect.size.height), kLineHorizontal, -1);
        CGPoint end = CGPointPathChange(CGPointMake(rect.size.width, rect.size.height), kLineHorizontal, -1);
        
        CGContextMoveToPoint(context, start.x, start.y);
        CGContextAddLineToPoint(context, end.x, end.y);
    }
    
    CGContextStrokePath(context);
    CGContextClosePath(context);
}

- (instancetype)init
{
    if (self =[super init]) {
        self.backgroundColor =[UIColor whiteColor];
    }
    return self;
 
}
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame]) {
         self.backgroundColor =[UIColor whiteColor];
    }
    return self;

}
@end
